package com.wwwarehouse.usercenter.fragment.permission_recycle;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.permission_recycle.RetrieveAuthorityBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoke.ni on 2017/10/12.
 */
@Route(path = "/UserCenter/PRUncollectedDetailFragment")
public class PRUncollectedDetailFragment extends BaseFragment {

    private CustomViewPagerInternal mCustomViewPagerInternal;
    private List<Fragment> mFragmentList;
    private MyFragmentPagerAdapter mViewPagerAdapter;
    private CirclePageIndicator mCirclePagerIndicator;
    private int mFragmentListSize;

    private StateLayout mStateLayout;
    private List<RetrieveAuthorityBean.DetailsBean> list;

    public static final int PAGE_SIZE = 10;

    public static PRUncollectedDetailFragment newInstance(List<RetrieveAuthorityBean.DetailsBean> list) {
        PRUncollectedDetailFragment fragment = new PRUncollectedDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("details", (ArrayList<? extends Parcelable>) list);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            list = bundle.getParcelableArrayList("details");
        }
        mFragmentList = new ArrayList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_page_detail_distribute_authority, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        // findViewById
        mCustomViewPagerInternal = $(R.id.cvpi);
        mCirclePagerIndicator = $(R.id.cpi);
        mStateLayout = $(R.id.slt);
        mStateLayout.showProgressView(true);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PRUncollectedDetailFragment) {
            mActivity.setTitle(getString(R.string.user_pr_unreceive_permission));
        }
    }

    @Override
    public void requestDatas() {
        mStateLayout.showContentView();
        if (list == null)
            return;
        int total = list.size();
        int remainder = total % PAGE_SIZE;
        if (remainder == 0) {
            mFragmentListSize = total / PAGE_SIZE;
        } else {
            mFragmentListSize = total / PAGE_SIZE + 1;
        }
        if (mFragmentListSize > 1) {
            for (int i = 0; i < mFragmentListSize; i++) {
                List<RetrieveAuthorityBean.DetailsBean> mDataList = new ArrayList<>();
                for (int j = 0; j < PAGE_SIZE; j++) {
                    if (i * PAGE_SIZE + j <list.size())
                        mDataList.add(list.get(i * PAGE_SIZE + j));
                }
                mFragmentList.add(PRUncollectedDetailListFragment.newInstance(i + 1 + "", mDataList));
            }
        } else {
            mFragmentList.add(PRUncollectedDetailListFragment.newInstance(1 + "", list));
        }
        mViewPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList);
        mCustomViewPagerInternal.setAdapter(mViewPagerAdapter);
        mCirclePagerIndicator.setViewPager(mCustomViewPagerInternal);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity) mActivity).hideBackBt();
    }


}
